package com.xybbs.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xybbs.entity.Replies;
import com.xybbs.entity.Users;
import com.xybbs.service.RepliesService;
import com.xybbs.service.TipsService;

@RequestMapping("reply")
@Controller
public class ReplyController {
	@Autowired
	RepliesService repliesService;
	@Autowired
	TipsService tipsService;
	
	@PostMapping("publishReply.do")
	public String publishReply(String tipId, Replies reply,HttpSession session) {
		Users user = (Users) session.getAttribute("user");
		reply.setReplyPublishTime(new Date());
		reply.setUserId(user.getUserId());
		reply.setTipId(Integer.parseInt(tipId));
		tipsService.updateTipRepliesWhenReplyed(tipId);
		repliesService.add(reply);
		return "forward:/tips/showTip.do";
	}
	@GetMapping("removeReply")
	public String removeReply(String replyId,String tipId) {
		repliesService.removeReplyById(replyId);
		tipsService.updateTipRepliesWhenRemoveReplyed(tipId);
		return "forward:/tips/showTip.do";
	}
	
	
}
